package zy.bluetooth.view.activity;

import android.graphics.Color;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import zy.bluetooth.R;
import zy.bluetooth.base.activity.BaseActivity;
import zy.bluetooth.base.bean.MessageBean;
import zy.bluetooth.base.constant.BltContant;
import zy.bluetooth.service.ReceiveSocketService;
import zy.bluetooth.service.SendSocketService;
import zy.bluetooth.utils.ToastUtil;
import zy.bluetooth.utils.factory.ThreadPoolProxyFactory;
import zy.bluetooth.widget.TitleBar;

public class ShowDataActivity extends BaseActivity {
    @BindView(R.id.titlebar)
    TitleBar titlebar;
    @BindView(R.id.content_ly)
    LinearLayout contentLy;
    @BindView(R.id.go_edit_text)
    EditText goEditText;
    @BindView(R.id.go_text_btn)
    Button goTextBtn;
    @BindView(R.id.go_file_btn)
    Button goFileBtn;
    @BindView(R.id.text)
    TextView text;
    @BindView(R.id.tv_num)
    TextView tvNum;
    private String date = "暂无数据";
    private String time = "暂无数据";
    private String smoke = "暂无数据";
    private String tem = "暂无数据";
    private String humidity = "暂无数据";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        titlebar.setTitle(getIntent().getStringExtra("devicename"));
        titlebar.setBackgroundResource(R.color.blue);
        titlebar.setImmersive(true);
        titlebar.setTitleColor(Color.WHITE);
        //开启消息接收端
        ThreadPoolProxyFactory.getNormalThreadPoolProxy().execute(new Runnable() {
            @Override
            public void run() {
                new ReceiveSocketService().receiveMessage();
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_show_data;
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    @OnClick({R.id.go_text_btn, R.id.go_file_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.go_text_btn:
                //发送文字消息
                if (TextUtils.isEmpty(goEditText.getText().toString())) {
                    ToastUtil.shortShow("请先输入信息");
                } else {
                    SendSocketService.sendMessage(goEditText.getText().toString());
                }
                break;
            case R.id.go_file_btn:
                SendSocketService.sendMessageByFile(Environment.getExternalStorageDirectory() + "/test.png");
                break;
        }
    }

    /**
     * RECEIVER_MESSAGE:21 收到消息
     * BltContant.SEND_TEXT_SUCCESS:发送消息成功
     * BltContant.SEND_FILE_NOTEXIT:文件不存在
     * BltContant.SEND_FILE_IS_FOLDER:不能发送文件夹
     *
     * @param messageBean
     */
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageBean messageBean) {
        switch (messageBean.getId()) {
            case 23:
                text.setText("刷新时间：" + formatter.format(new Date()));
                break;
            case 21:
//                Log.d("收到消息", messageBean.getContent());
                String content = messageBean.getContent();
//                tvNum.setText(content);
//                text.append(content + "当前时间" + formatter.format(new Date())+"\n");
                String[] split = content.split(",");
                for (int i = 0; i < split.length; i++) {
                    if (split[i].startsWith("D")) {
                        date = split[i].substring(1);
                    } else if (split[i].startsWith("T")) {
                        time = split[i].substring(1);
                    } else if (split[i].startsWith("M")) {
                        smoke = split[i].substring(1);
                    } else if (split[i].startsWith("P")) {
                        tem = split[i].substring(1);
                    } else if (split[i].startsWith("H")) {
                        humidity = split[i].substring(1);
                    }
                }
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append("日期:" + date.replace(".", "-") + "\n");
                stringBuilder.append("时间:" + time.replace(".", ":") + "\n");
                stringBuilder.append("烟雾浓度:" + smoke + " PPM\n");
                stringBuilder.append("温度:" + tem + " ℃\n");
                stringBuilder.append("湿度:" + humidity + " %\n");
                tvNum.setText(stringBuilder.toString());
                break;
            case BltContant.SEND_TEXT_SUCCESS:
                text.append("我:" + goEditText.getText().toString() + "\n");
                goEditText.setText("");
                break;
            case BltContant.SEND_FILE_NOTEXIT:
                ToastUtil.shortShow("发送的文件不存在，内存根目录下的test.png");
                break;
            case BltContant.SEND_FILE_IS_FOLDER:
                ToastUtil.shortShow("不能传送一个文件夹");
                break;
            default:
                break;
        }
    }
}
